//
//  UIPatientInformationInputTextField.m
//  vhStressTest1
//
//  Created by jia yu on 2021/8/31.
//

#import "UIStandardInputTextField.h"
#import <vhECGTools/vhECGTools.h>
@interface UIStandardInputTextField()<UITextFieldDelegate>{
    float screenRatio;
    UIColor *failedBgColor;
    UIColor *failedBdColor;
    UIColor *successBgColor;
    UIColor *successBdColor;
    UIColor *defaultBgColor;
    UIColor *defaultBdColor;
    UIColor *disableTextColor;
    UIColor *disableBgColor;
    UIColor *editableTextColor;
}
@end

@implementation UIStandardInputTextField
@synthesize changedHandler;
@synthesize regex;
@synthesize style;
@synthesize helpString;
@synthesize editable;

-(void)setEditable:(BOOL)_editable{
    editable = _editable;
    if (NO == editable){
        self.textColor = [vhECGTools colorFromHEXString:@"294559" withAlpha:0.4];
        self.backgroundColor = [vhECGTools colorFromHEXString:@"f7fafc" withAlpha:0.6];
        self.layer.borderColor = [vhECGTools colorFromHEXString:@"dde0e2" withAlpha:0.6].CGColor;
        self.userInteractionEnabled = NO;
    }
}

-(void)setEnabled:(BOOL)_enabled{
    [super setEnabled:_enabled];
    if (self.enabled){
        self.textColor = editableTextColor;
        self.backgroundColor = defaultBgColor;
    }else{
        self.textColor = disableTextColor;
        self.backgroundColor = disableBgColor;
    }
    return;
}

-(void)setHelpString:(NSString *)_helpString{
    helpString = _helpString;
    if (@available(macCatalyst 15.0, *)) {
        self.toolTip = helpString;
    }
}

-(void)setStyle:(UIStandardInputTextField_Style)_style{
    style = _style;
    switch (style) {
        case UIStandardInputTextField_Style_Default:
            self.font = [UIFont fontWithName:@"PingFangSC-Medium" size:34.f * screenRatio];
            self.layer.borderWidth = 0.f;
            break;
        case UIStandardInputTextField_Style_WithBoard:
            self.font = [UIFont fontWithName:@"PingFangSC-Regular" size:34.f * screenRatio];
            self.layer.borderWidth = 2.3f * screenRatio;
            self.layer.borderColor = defaultBdColor.CGColor;
            break;
        default:
            break;
    }
}

-(id)initWithFrame:(CGRect)frame{
    return [[UIStandardInputTextField alloc] initWithFrame:frame scale:1.f];
}

-(id)initWithFrame:(CGRect)frame scale:(float)scale{
    self = [super initWithFrame:frame];
    if (self){
        self.style = UIStandardInputTextField_Style_Default;
        editable = YES;
        screenRatio = scale;
        regex = nil;
        self.font = [UIFont fontWithName:@"PingFangSC-Medium" size:34.f * screenRatio];
        
//        defaultBgColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1.f];
//        defaultBdColor = [UIColor colorWithRed:0.84 green:0.85 blue:0.86 alpha:1.f];
        defaultBgColor = [vhECGTools colorFromHEXString:@"f7fafc"];
        defaultBdColor = [vhECGTools colorFromHEXString:@"dde0e2"];
        
//        failedBgColor = [UIColor colorWithRed:1.f green:0.92 blue:0.93 alpha:1.f];
        failedBgColor = [vhECGTools colorFromHEXString:@"fdeff1"];
//        failedBdColor = [UIColor colorWithRed:0.91 green:0.21 blue:0.35 alpha:1.f];
        failedBdColor = [vhECGTools colorFromHEXString:@"ec6b7d"];
        
//        successBgColor = [UIColor colorWithRed:0.96 green:0.98 blue:0.99 alpha:1.f];
        successBgColor = [vhECGTools colorFromHEXString:@"f5f9fa"];
//        successBdColor = [UIColor colorWithRed:0.11 green:0.6 blue:0.6 alpha:1.f];
        successBdColor = [vhECGTools colorFromHEXString:@"57abaf"];

//        disableTextColor = [UIColor colorWithRed:0.66 green:0.66 blue:0.66 alpha:1.f];
        disableTextColor = [vhECGTools colorFromHEXString:@"294559" withAlpha:0.4];
        disableBgColor = [vhECGTools colorFromHEXString:@"f7fafc" withAlpha:0.4];
//        editableTextColor = [UIColor colorWithRed:0.1 green:0.21 blue:0.28 alpha:1.f];
        editableTextColor = [vhECGTools colorFromHEXString:@"294559"];
        
        self.textColor = editableTextColor;
        
        self.backgroundColor = defaultBgColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 10.f * screenRatio;
        self.delegate =self;
        
        self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, 26.f * screenRatio, frame.size.height)];
        self.leftView.backgroundColor = [UIColor clearColor];
        self.leftViewMode = UITextFieldViewModeAlways;
    }
    return self;
}

-(BOOL)available{
    if (nil == regex){
        return YES;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self.text];
}

-(BOOL)becomeFirstResponder{
    self.layer.borderWidth = 2.3 * screenRatio;
    return [super becomeFirstResponder];
}

-(BOOL)resignFirstResponder{
    self.backgroundColor = defaultBgColor;
    if (NO == [self available]){
        self.backgroundColor = failedBgColor;
        self.layer.borderColor = failedBdColor.CGColor;
    }else{
        switch (style) {
            case UIStandardInputTextField_Style_WithBoard:
                self.layer.borderColor = defaultBdColor.CGColor;
                self.layer.borderWidth = 2.3 * screenRatio;
                break;
            default:
                self.layer.borderWidth = 0.f;
                break;
        }
    }
    return [super resignFirstResponder];
}


#pragma mark - UITextFieldDelegate
-(void)textFieldDidChangeSelection:(UITextField *)textField{
    if (changedHandler){
        changedHandler(textField.text);
    }
    if ([self available]){
        self.backgroundColor = successBgColor;
        self.layer.borderColor = successBdColor.CGColor;
    }else{
        self.backgroundColor = failedBgColor;
        self.layer.borderColor = failedBdColor.CGColor;
    }
    return;
}

@end
